package com.mutual.assistance.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;



/**
 * @author chenyuxin
 * @version 1.0
 * @description 自动映射数据库生成字段
 * @date 2023/4/6 23:24
 */
public class Generator {

    public static void main(String[] args) {
        //generator(GeneratorConstant.MEDIA_URL, "media");
        //generator(GeneratorConstant.USER_URL, "user");
        //generator(GeneratorConstant.SYSTEM_URL, "system");
        generator(GeneratorConstant.TASK_URL, "task");
    }

    private static void generator(String url, String moduleName) {
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(url,
                GeneratorConstant.USERNAME, GeneratorConstant.PASSWORD)
                .dbQuery(new MySqlQuery()) // 数据库查询
                //        .schema("mybatis-plus")
                .typeConvert(new MySqlTypeConvert()) // 数据库类型转换器
                .keyWordsHandler(new MySqlKeyWordsHandler()); // 数据库关键字处理器


        FastAutoGenerator.create(dataSourceConfigBuilder)
                .globalConfig(builder -> {
                    builder.author("") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .disableOpenDir() // 完成之后不开启弹窗
                            .outputDir(System.getProperty("user.dir") + "/mybatis-plus-generator/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.mutual.assistance.generator") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .entity("model.po");
                })
                .strategyConfig(builder -> {
                    builder
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .controllerBuilder()
                            .enableHyphenStyle()
                            .enableRestStyle()
                            .mapperBuilder()
                            .enableBaseColumnList()
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            //.enableRemoveIsPrefix()
                            .build();
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
